package com.project.controller;

import com.project.pojo.User;
import com.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    @RequestMapping("/allUser")
    public String list(Model model){
        List<User> list = userService.queryAllUser();
//        for (User users : list) {
//            System.out.println(users);
//        }
        model.addAttribute("list" , list);
        return "allUser";
    }

    @RequestMapping("/toAddUser")
    public String toAddPaper() {
        return "addUser";
    }

    @RequestMapping("/addUser")
    public String addPaper(User user) {
//        System.out.println(user);
        userService.addUser(user);
        return "redirect:/user/allUser";
    }


    @RequestMapping("/toUpdateUser")
    public String toUpdateUser(Model model, int id) {
        User users = userService.queryUserById(id);
    //    System.out.println(users);
        model.addAttribute("user",users );
        return "updateUser";
    }

    @RequestMapping("/updateUser")
    public String updateUser(Model model, User user) {
   //     System.out.println(user);
        userService.updateUser(user);
        User users = userService.queryUserById(user.getUserID());
        model.addAttribute("users", users);
        return "redirect:/user/allUser";
    }


    @RequestMapping("/del/{UserId}")
    public String deleteBook(@PathVariable("UserId") int id) {
        userService.deleteUserById(id);
        return "redirect:/user/allUser";
    }
    //查询书籍ser
    @RequestMapping("/queryUser")
    public String queryUser(String queryUserName,Model model){
        List<User> users = userService.queryUserName(queryUserName);


        model.addAttribute("list",users);

        return "allUser";
    }
    @RequestMapping("/commit")
    public String callback(){
        return "callback";
    }
}
